package com.zzl.car.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.car.entity.CarBaseInfo;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.service.ZzwCarBaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

@RestController
@CrossOrigin
public class ZzwCarBaseInfoController {

    @Autowired
    private ZzwCarBaseInfoService zzwCarBaseInfoService;

    /**
     * 添加车辆基础信息
     * @param map   实体对象
     * @param image 图片信息
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadCarBaseInfo")
    public String addCarBaseInfo(@RequestParam Map<String, Object> map,
                                 @RequestParam(value = "image", required = false) MultipartFile image) throws IOException {

        if (image == null) {
            throw new ZdpException(4915, "请添加图片");
        }
        System.out.println("图片初始名-->" + image.getOriginalFilename());
        System.out.println(map);

        return zzwCarBaseInfoService.addCarBaseInfo(map, image);
    }

    /**修改车辆基础信息
     * @param map
     * @param image 要修改的图片信息
     * @return
     * @throws IOException
     */
    @PostMapping("/modifyCarBaseInfo")
    public String modifyCarBaseInfo(@RequestParam Map<String, Object> map,
                                 @RequestParam(value = "image", required = false) MultipartFile image) throws IOException {

        System.out.println(map);

        return zzwCarBaseInfoService.modifyCarBaseInfo(map, image);
    }

    // 得到车辆所有信息 --> 分页
    @GetMapping("/getAllCarBaseInfoByPage")
    public PageInfo<CarBaseInfo> getAllCarBaseInfoByPage(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
        System.out.println("currentPage-->" + currentPage);
        return zzwCarBaseInfoService.getAllCarBaseInfoPage(currentPage, 5);
    }

    // 得到车辆所有信息 --> 不分页
    @GetMapping("/getAllCarBaseInfo")
    public List<CarBaseInfo> getAllCarBaseInfo() {
        return zzwCarBaseInfoService.getAllCarBaseInfo();
    }

    // 删除车辆信息
    @GetMapping("/deleteCarBaseInfo")
    public String deleteCarBaseInfo(@RequestParam("id") Integer id) {
        System.out.println("deleteCarBaseInfo");
        return zzwCarBaseInfoService.deleteCarBaseInfo(id);
    }

    // 条件查询 brand detailType
    @GetMapping("/searchCarBaseInfo")
    public PageInfo<CarBaseInfo> searchCarBaseInfo(@RequestParam("brand")String brand,
                                                   @RequestParam("detailType")String detailType,
                                                   @RequestParam(value = "currentPage",defaultValue = "1") int currentPage){

        return zzwCarBaseInfoService.searchCarBaseInfo(brand,detailType,currentPage);
    }
}
